# Maintainer: Stephen Groat <stephen@egroat.com>

_realname=cmocka
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.5
pkgrel=3
pkgdesc="Unit testing framework for C (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:Apache-2.0')
url="https://cmocka.org/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://cmocka.org/files/1.1/${_realname}-${pkgver}.tar.xz"
        "001-install-fixes.patch"
        "002-dll-name.patch"
        "003-fix-static-libname.patch"
        "004-fix-pkgconfig-files.patch")
sha256sums=('f0ccd8242d55e2fd74b16ba518359151f6f8383ff8aef4976e48393f77bba8b6'
            'd0ce41c681a87741883d801ef2f1d79c7a630fe974543ae001d29bfc1aea1925'
            'b224f3906cdd3c90fd6e08e51f9d26bc0e7160f3d78a0e6fc520e9b054f39731'
            '404138b407567ea4b89de8d4bffd0bba2879806761d606242b41c79c9e1efb1d'
            '430e9647bc7addaea593f9efb72394888ef8c386d631ee6fed5dd56cc12416f8')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/001-install-fixes.patch"
  patch -p1 -i "${srcdir}/002-dll-name.patch"
  patch -p1 -i "${srcdir}/003-fix-static-libname.patch"
  patch -p1 -i "${srcdir}/004-fix-pkgconfig-files.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DWITH_STATIC_LIB=ON \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DWITH_CMOCKERY_SUPPORT=ON \
    -DUNIT_TESTING=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd ${srcdir}/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/ctest || true
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
